﻿//包目标对象
packageContent.effectObj = function(actionItemId,effectObjID,partObjId,actionType,partObjCD) {
	var _actionItemId = actionItemId;
	var _effectObjID = effectObjID;
	var _partObjId = partObjId;//对象ID
	var _partObjCD = partObjCD;//对象number
	var _actionType = actionType;

	var _initEffectObjComp = function() {
		$("#selectOfferSpec").window( {
			title : '选择销售品规格',
			width : 700,
			height : 420,
			modal : true,
			shadow : false,
			closed : true,
			resizable : false,
			draggable : true,
			minimizable : false,
			maximizable : false
		});
		if(ACTION.TYPE.D == _actionType) {
			$("#p_effectObj_role").empty();
			$("#p_effectObj_role").attr('disabled','true'); //不可用
			$("#p_effectObj_txt").removeAttr("ROLENAME");
			$("#p_effectObj_txt").removeAttr("ROLECD");
		}

	}
	var _initEffectObj = function() {
		//基本信息赋值
		if ((pack.getPackObjMap() != undefined)
				&& (pack.getPackObjMap().get(_effectObjID) != null)) {
			//展示目标对象名
			var _packageObj = pack.getPackObjMap().get(_effectObjID);
			$("#p_effectObj_txt").val(_packageObj.getObjectName());
		}

		if ((pack.getActionItemMap() != undefined)
				&& (pack.getActionItemMap().get(_actionItemId) != null)) {
			var _packActionItem = pack.getActionItemMap().get(_actionItemId);
			//角色对象
			var ROLENAME = _packActionItem.getRoleName();
			var ROLECD = _packActionItem.getRoleCd();

			$("#p_effectObj_role").empty();
			var appendOption = $("<option>");
			appendOption.val(ROLECD);
			appendOption.html(ROLENAME);
			$("#p_effectObj_role").append(appendOption);
			$("#p_effectObj_role").val(ROLECD);

			//赋值
			$("#p_effectObj_txt").attr("OSPEC_ID",_effectObjID);
			$("#p_effectObj_txt").attr("ROLENAME",ROLENAME);
			$("#p_effectObj_txt").attr("ROLECD",ROLECD);
		}


	};

	var _validate = function(type) {
		//查询前的校验
		if (type == VALIDATE.TYPE.QUERY) {
			if (_partObjId == "undefined") {
				alert("查询前请设置【参与对象】");
				return false;
			}
			if ( _actionType == "undefined") {
				alert("查询前请设置【动作】");
				return false;
			}


			//客户订购 的【目标对象】被引用时，不能修改
			if (_partObjId == OBJECT.PARTY) {
				var ss =$('span[effectobjid="'+ _effectObjID +'"]');
				if (ss.length > 1) {
					alert('【目标对象】被引用不能修改'); ;
					return false;
				}
			}

			//动作【加入】时
			if (_actionType == ACTION.TYPE.J) {
				//查询前需【客户】订购销售品
				//1.是否有客户节点
				var partyEffectObj = $("span[objId=-1]");
				if (partyEffectObj && partyEffectObj.length == 0) {
					PACK.ALERT.warn("产品做【加入】动作，请先通过【客户】订购销售品");
					return false;
				}

				//2.客户是否订购
				var tempObjId = [];
				var itemId = partyEffectObj.parents("tr").attr("id");
				var trs = "#pageBody tr[id^='"+itemId.substring(0,itemId.lastIndexOf("_"))+"_']";
				$(trs).each(function() {
					var effectobjid = $($(this).next().find("td")[1]).find("span").attr("effectobjid");
					if (effectobjid) {
						tempObjId.push(effectobjid);
					}
				});
				var offerSpecIds = tempObjId.join(",");
				if (offerSpecIds == "") {
					PACK.ALERT.warn("产品做【加入】动作，请先通过【客户】订购销售品");
					return false;
				}
			}
		}


		//保存前的校验
		if (type == VALIDATE.TYPE.SAVE ) {
			if (!$("#p_effectObj_txt").val()) {
				alert('请查询设置【目标对象】'); ;
				return false;
			}

			//客户订购 的【目标对象】不能重复
			if (_partObjId == OBJECT.PARTY) {
				var packObjMap = pack.getPackObjMap();
				var tempObj = [];
				var _isHave = false;
				var tempObjId = $("#p_effectObj_txt").attr("OSPEC_ID")
				if (packObjMap != undefined) {
					var objValues = packObjMap.values();
					var obj;
					for(var j = 0;j < packObjMap.size();j++) {
						obj = objValues[j];
						if (obj.getObjId() == tempObjId) {
							_isHave = true;
							break ;
						}
					}
				}
				if (_isHave) {
					alert("客户订购的【目标对象】不能重复");
					return false;
				}
			}
		}



		return true;
	}
	//打开查询查询面板
	var _openQueryWindow = function() {
		if (!_validate(VALIDATE.TYPE.QUERY)) {
			return ;
		}
		$("#selectOfferSpec").window('open');
		$('#selectOfferSpec').window('refresh',
				'PackagePathDispatcher?path=selectOfferSpec.jsp?partObjId='+_partObjId+"&actionType="+_actionType);
	}

	//保存
	var _saveEffectObj = function() {
		//校验
		var flag = _validate(VALIDATE.TYPE.SAVE) ;
		if (!flag)  {
			return flag;
		}

		var objName = $("#p_effectObj_txt").val();
		var effectObjId = $("#p_effectObj_txt").attr("OSPEC_ID");
		var ROLENAME = $("#p_effectObj_txt").attr("ROLENAME");
		var ROLECD = $("#p_effectObj_txt").attr("ROLECD");
		var objType = $("#p_effectObj_txt").attr("OBJ_TYPE");

		//var objType = OBJECT.OBJTYPE.OFFER_SPEC;

		//重新赋值:目标对象
		var _packageObj;
		var packObjMap = pack.getPackObjMap();
		if (packObjMap == undefined) {
			packObjMap = new HashMap();
		} else {
			packObjMap = pack.getPackObjMap();
		}


		if (operateType == PACKAGE.OPERATE.NEW) {
			if (!(packObjMap.get(_effectObjID) == null)) {
				_packageObj = pack.getPackObjMap().get(_effectObjID);
			} else {
				_packageObj = new PackageObject();
			}

			_packageObj.setObjId(effectObjId);
			_packageObj.setObjType(objType);
			_packageObj.setObjectName(objName);
			_packageObj.setObjectNumber(effectObjId);

			packObjMap.put(effectObjId, _packageObj);
			pack.setPackObjMap(packObjMap);
		} else {

			//修改时未做变更，保存时，直接返回
			if (operateType == PACKAGE.OPERATE.MODIFY) {
				if ((_effectObjID == effectObjId)) {
					_doClose();
					return ;
				}
			}

			//修改时，判断对象在内容中是否存在
			var packObjMap = pack.getPackObjMap();
			var tempObj = [];
			var _isHave = false;
			if (packObjMap != undefined) {
				var objValues = packObjMap.values();
				var obj;
				for(var j = 0;j < packObjMap.size();j++) {
					obj = objValues[j];
					if (obj.getObjId() == effectObjId) {
						_isHave = true;
						effectObjId = obj.getObjectNumber();
						break ;
					}
				}
				if (!_isHave) {
					_packageObj = new PackageObject();
					_packageObj.setObjId(effectObjId);
					_packageObj.setObjType(objType);
					_packageObj.setObjectName(objName);
					_packageObj.setObjectNumber(effectObjId);

					packObjMap.put(effectObjId, _packageObj);
					pack.setPackObjMap(packObjMap);
				}
			}
		}


		//重新赋值:动作
		var _packActionItem;
		var actionItemMap = pack.getActionItemMap();
		if (actionItemMap == undefined) {
			actionItemMap = new HashMap();
			pack.setActionItemMap(actionItemMap)
		} else {
			actionItemMap = pack.getActionItemMap();
		}
		if (!(pack.getActionItemMap().get(actionItemId) == null)) {
			_packActionItem = pack.getActionItemMap().get(actionItemId);
		} else {
			_packActionItem = new PackageActionItem();
		}
		//分模块完善信息
		_packActionItem.setActionItemId(_actionItemId);
		_packActionItem.setActionTypeCd(_actionType);
		_packActionItem.setPartobjNumber(_partObjCD);
		_packActionItem.setEffectobjNumber(effectObjId);
		_packActionItem.setRoleCd(ROLECD);
		_packActionItem.setRoleName(ROLENAME);
		actionItemMap.put(_actionItemId,_packActionItem);

		pack.setActionItemMap(actionItemMap);



		//整体逻辑 ：第二列 第2个
		var itemActionTd = $("#" + _actionItemId).next().find("td")[1];
		$(itemActionTd).find("span").attr("title",objName);
		if(objName.length > 18) {
			objName = objName.substring(0,18)+"...";
		}
		$(itemActionTd).find("span").html(objName);
		$(itemActionTd).find("span").attr("effectObjID",effectObjId);

		return flag;
	}

	var _initButton = function() {
		$("#p_effectObj_save").bind("click", function() {
			var flag = _saveEffectObj();
			if (flag) {
				_doClose();
			}
		});
		$("#p_effectObj_cancel").bind("click", function() {
			_doClose();
		});
		$("#p_effectObj_search").bind("click", function() {
			_openQueryWindow();
		});
		$("#p_effectObj_txt").bind("dblclick", function() {
			_openQueryWindow();
		});

		$("#p_effectObj_save").addClass("btn-blue");
		$("#p_effectObj_cancel").addClass("btn-gray");
	}

	//关闭
	var _doClose = function() {
		$("#openEffectObjPage").window('close');
	}

	//初始化包参与对象
	var _init = function() {
		_initEffectObjComp();
		_initEffectObj();
		_initButton();
	}

	_init();
}
